package State
{
	import ResourcePackage.ExternalResource;
	import ResourcePackage.ResourceMovieClip;
	
	import core.*;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.net.*;
	import flash.text.*;
	import flash.utils.ByteArray;
	
	import flashx.textLayout.formats.TextAlign;
	
	public class GSBattle extends State
	{
		//public static var s_player1Name:String;
		//public static var s_player2Name:String;
		[Embed(source="../rsc/battle.swf", mimeType="application/octet-stream")]
		public static var BattleSprite:Class;
		public var battltRscSprite:ResourceMovieClip = new ResourceMovieClip();
		
		[Embed(source="../rsc/char_1.swf", mimeType="application/octet-stream")]
		public static var Char1Sprite:Class;
		public var char1RscSprite:ResourceMovieClip = new ResourceMovieClip();
		
		public static var s_match:MatchDTO;
		private var m_actionTurn:int;
		private var m_hitGroundText1:MovieClip;
		private var m_hitGroundText2:MovieClip;
		private var m_hitGroundText3:MovieClip;
		
		private var m_sprPlayer1:MovieClip;
		private var m_player1PosX:int;
		private var m_player1PosY:int;
		
		private var m_sprPlayer2:MovieClip;
		private var m_player2PosX:int;
		private var m_player2PosY:int;
		
		private var m_bAniming:Boolean;
		private var m_bLoadComplete:Boolean;
		
		private var m_currentMove:MoveDTO;
		
		
		private var m_gameTime:int;
		private var m_gameTicker:int;
		private var m_LastRefresh:int;
		
		public function GSBattle()
		{
			super();
		}

		
		public override function Render(g:MyGraphics):void
		{
			if(!m_bLoadComplete)
				return;
			
			g.SetTextAlign(TextAlign.LEFT);
			g.SetTextSize(16);
			g.DrawString(s_match.PlayerName1,5,5,0x0000ff);
			g.DrawString(s_match.PlayerName2,Define.SCREEN_WIDTH - 100,5,0x0000ff);
			
			g.DrawMovieClip(m_sprPlayer1,m_player1PosX,m_player1PosY);
			
			
			g.DrawMovieClip(m_sprPlayer2,m_player2PosX,m_player2PosY);
			//Game.GetMyGraphics().DrawMovieClip(m_sprPlayer2,m_player2PosX,m_player2PosY);
			
		}
		public override function Update(dt:int):void
		{
			if(!m_bLoadComplete)
				return;
			
			if(CGame.GetGame().m_bIsConnecting)
				return;
			m_gameTicker += dt;
			if(m_gameTicker >=1000)
			{
				m_gameTime += m_gameTicker/1000;
				m_gameTicker = m_gameTicker%1000;
			}
			
			if(m_bAniming)
			{
				if(s_match.step == 1)
				{
					if(m_player1PosX < Define.SCREEN_WIDTH * 4/5 - 2 * m_sprPlayer1.width - 30)
					{
						m_player1PosX +=15;
						return;
					}
				}
				return;
			}
			if(s_match.turn % 2 == m_actionTurn)
			{
				var post:FBHttpPost;
				var data:String
				
				if(s_match.step == 0)
				{
					
				}
				else if(s_match.step < 4)
				{
					for(var i:int;i<3;i++)
					{
						if(CGame.GetGame().m_keypad.HasKeyBeenReleased(i))
						{
							post = new FBHttpPost(CallBack_BattleMove);				
							post.Post(Define.URLBattle,								
								"matchid=" + s_match.id + 
								"$turn=" + s_match.turn +
								"$step=" + s_match.step + 
								"$dicetype=5" +
								CGame.GetGame().AuthenticateKey());
							CGame.GetGame().m_bIsConnecting = true;
							m_LastRefresh = m_gameTime;
							
							//m_bAniming = true;
							//UnRegisterButton();
							
							break;
						}
					}
				}
				else if(s_match.step < 5)
				{
					
				}
					
			}
		}
		
		public function CallBack_BattleMove(data:String,b:Boolean):void
		{
			CGame.GetGame().m_bIsConnecting = false;
			
			if(b == false)
			{
				return;
			}
			
			var variables:URLVariables = new URLVariables(data);
			if (variables.error != null)
			{
				var state:State = CGame.CreateState(1);
				if(state)
				{
					Game.GetStateStack().SwitchState(state,false);	
				}
			}
			else if (variables.diceresult > 0)
			{
				m_currentMove = new MoveDTO();
				m_currentMove.index = s_match.turn*5 + s_match.step;
				m_currentMove.turn = s_match.turn;
				m_currentMove.step = s_match.step;
				//m_currentMove.dicetype = variables.dicetype;
				m_currentMove.diceresult = variables.diceresult;
				//m_currentMove.skill = variables.skill;
				
				m_bAniming = true;
				UnRegisterButton();
			}
			
			
			
		}
		public override function Init():void
		{
		
			var s:Vector.<ExternalResource> = new Vector.<ExternalResource>();
			
			s.push(new ExternalResource(battltRscSprite, ExternalResource.TYPE_FromByteArray, new BattleSprite()));
			s.push(new ExternalResource(char1RscSprite, ExternalResource.TYPE_FromByteArray, new Char1Sprite()));
			
			Game.GetStateStack().Push(new StateLoading("Init",s,this, false, false));
			
			m_player1PosX = Define.SCREEN_WIDTH/5;
			m_player1PosY = Define.SCREEN_HEIGHT *2/3;			
			m_player2PosX = Define.SCREEN_WIDTH * 4/5;
			m_player2PosY = Define.SCREEN_HEIGHT *2/3;
			
			m_bLoadComplete = false;
			m_LastRefresh = 0;
		}
		
		public function RegisterButton():void
		{
			if(s_match.step >0 && s_match.step < 4)
			{
				CGame.GetGame().m_keypad.GetButton(0).SetText("5/6");
				CGame.GetGame().m_keypad.GetButton(1).SetText("3/6");
				CGame.GetGame().m_keypad.GetButton(2).SetText("1/6");				
				CGame.GetGame().m_keypad.GetButton(0).Enable(true);
				CGame.GetGame().m_keypad.GetButton(1).Enable(true);
				CGame.GetGame().m_keypad.GetButton(2).Enable(true);
			}
		}
		
		public function UnRegisterButton():void
		{
			if(s_match.step >0 && s_match.step < 4)
			{
				CGame.GetGame().m_keypad.GetButton(0).Enable(false);
				CGame.GetGame().m_keypad.GetButton(1).Enable(false);
				CGame.GetGame().m_keypad.GetButton(2).Enable(false);
				
			}
		}
		
		
		public function PostInit():void
		{
			m_hitGroundText1 = battltRscSprite.GetMovieClip().CreatetGroundText();
			m_hitGroundText2 = battltRscSprite.GetMovieClip().CreatetGroundText();
			m_hitGroundText3 = battltRscSprite.GetMovieClip().CreatetGroundText();
			
			m_sprPlayer1 = char1RscSprite.GetMovieClip().CreateChar1R();
			m_sprPlayer2 = char1RscSprite.GetMovieClip().CreateChar1L();

			if(CGame.GetGame().m_playerName == s_match.PlayerName1)
				m_actionTurn = 0;
			else if(CGame.GetGame().m_playerName == s_match.PlayerName2)
				m_actionTurn = 1;
			else
				m_actionTurn = -1;
			
			if(s_match.turn % 2 == m_actionTurn)
			{
				CGame.GetGame().m_keypad.AddButton(0,100,50,m_hitGroundText1,"5/6");
				CGame.GetGame().m_keypad.AddButton(1,100,85,m_hitGroundText2,"3/6");
				CGame.GetGame().m_keypad.AddButton(2,100,120,m_hitGroundText3,"1/6");
				CGame.GetGame().m_keypad.GetButton(0).Enable(false);
				CGame.GetGame().m_keypad.GetButton(1).Enable(false);
				CGame.GetGame().m_keypad.GetButton(2).Enable(false);
				
				if(s_match.turn % 2 == m_actionTurn &&
					s_match.step > 0 &&s_match.step < 4)
						RegisterButton();
			}
			m_bAniming = false;
			
			m_gameTime = 0;
			m_gameTicker = 0;
			m_LastRefresh = m_gameTime;
			
		}
		public override function Release():void
		{
			
		}
		

		
		public override function LoadComplete(success:Boolean, description:String):void
		{
			if(success)
			{
				switch(description)
				{
					case "Init":											
						PostInit();
						break;
					
				}
				
				m_bLoadComplete = true;
			}
		}
		public override function GetStateName():String
		{
			return "GSBattle";
		}
	}
}